package com.reflex;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;

public class ReflexDemo01 {
    public static void main(String[] args) {
        // 获取String的Class对象
        Class<String> stringClass = String.class;
        //获取ArrayList的Class对象
        //Class<ArrayList> arrayListClass = ArrayList.class;
        System.out.println(stringClass);

        try {
            //第二种获取Class对象的方式
            Class<?> aClassString = Class.forName("java.lang.String");
            Class<?> aClassBufferedInputStream = Class.forName("java.io.BufferedInputStream");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        System.out.println("----------------");

        try {
            //获取自定义Person类对象表示的完全名称
            Class<?> aClassPerson = Class.forName("com.reflex.Person");
            Method[] methods = aClassPerson.getMethods();
            for (Method method : methods) {
                System.out.println(method.getName());
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        System.out.println("----------------");

        //获取类对象表示的完全名称
        String name = stringClass.getName();
        System.out.println("name = " + name);

        //获取类对象表示的简单名称
        String simpleName = stringClass.getSimpleName();
        System.out.println("simpleName = " + simpleName);

        //获取包对象
        Package aPackage = stringClass.getPackage();
        //获取包名称
        String name1 = aPackage.getName();
        System.out.println("name1 = " + name1);

        //获取所有方法
        Method[] methods = stringClass.getMethods();
        //获取方法数量
        System.out.println("methods.length = " + methods.length);
        //遍历方法并打印方法名称
        for (Method method : methods) {
            System.out.println(method.getName());
        }

        //获取所有构造方法
        Constructor<?>[] constructors = stringClass.getConstructors();
        //获取构造方法数量
        System.out.println("constructors.length = " + constructors.length);
        //遍历构造方法并打印构造方法
        for (Constructor<?> constructor : constructors) {
            System.out.println(constructor);
        }
    }
}
